package com.atguigu.tingshu.search.factory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleTaskThreadPoolFactory {

    private static final ScheduledExecutorService scheduledExecutorService;

    // 加载这个类的时候 提前将 线程池对象创建好
    static {
        scheduledExecutorService = Executors.newScheduledThreadPool(2);
    }

    private ScheduleTaskThreadPoolFactory() {
    }

    public void execute(Runnable runnable, long ttl, TimeUnit timeUnit) {
        scheduledExecutorService.schedule(runnable, ttl, timeUnit);
    }

    private static final ScheduleTaskThreadPoolFactory INSTANCE = new ScheduleTaskThreadPoolFactory();

    public static ScheduleTaskThreadPoolFactory getInstance() {
        return INSTANCE;
    }

    /**
     * 计算时间差
     */
    public static Long diffTime(Long currentTime) {
        // 算出当前时间的七天后的凌晨两点
        LocalDate localDate = LocalDate.now().plusDays(7);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(2, 0, 0));

        long time = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return time - currentTime;
    }
}
